//package com.atguigu.springcloud.alibaba.myhandler;
//
//import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor;
//import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
//import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner;
//import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
//import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
//import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
//import com.alibaba.csp.sentinel.util.StringUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerMapping;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.List;
//import java.util.Optional;
//
//@Slf4j
//@Component
//public class MyAuthority implements RequestOriginParser {
//
//
//    @Override
//    public String parseOrigin(HttpServletRequest request) {
//        //获取路径
//        Optional<String> resouceName = Optional.ofNullable(getResourceName(request));
//        log.debug("当前请求的资源是：{}", resouceName);
//        //获取ip
//        Optional<String> ip = Optional.ofNullable(request.getRemoteAddr());
//        log.debug("当前请求的来源ip是：{}", ip);
//        //获取servicename
//        Optional<String> servicename = Optional.ofNullable(request.getHeader("servicename"));
//        log.debug("当前请求的来源服务是：{}", servicename);
//
//        //返回的limitapp值,默认应该是default
//        String limitApp = "default";
//
//        //没有获取到resouceName直接返回default
//        if (!resouceName.isPresent()) {
//            return limitApp;
//        }
//        //校验规则,看返回什么值
//        List<AuthorityRule> rules = AuthorityRuleManager.getRules();
//        for (AuthorityRule authorityRule : rules) {
//            //如果有resource匹配的规则，规则来源是ip类型，返回ip
//            if (authorityRule.getResource().equals(resouceName.get())) {
//                if (limitAppIsIp(authorityRule.getLimitApp())) {
//                    limitApp = ip.orElse("localhost");
//                } else {
//                    limitApp = servicename.orElse("default");
//                }
//            }
//        }
//        //如果没有匹配的规则，返回default
//        return limitApp;
//    }
//
//
//    /**
//     * @Description:传入limitapp，判断是不是ip类型的
//     * @Param: [limitApp]
//     * @return: boolean
//     * @Author: Mr.zengbang
//     * @Date: 2021/2/4
//     */
//    protected boolean limitAppIsIp(String limitApp) {
//        if (limitApp.indexOf(".") != -1) {
//            return true;
//        } else {
//            return false;
//        }
//    }
//
//    /**
//     * @Description:根据request拿到resourcename
//     * @Param: [request]
//     * @return: java.lang.String
//     * @Author: Mr.zengbang
//     * @Date: 2021/2/4
//     */
//    protected String getResourceName(HttpServletRequest request) {
//        // Resolve the Spring Web URL pattern from the request attribute.
//        Object resourceNameObject = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
//        if (resourceNameObject == null || !(resourceNameObject instanceof String)) {
//            return null;
//        }
//        String resourceName = (String) resourceNameObject;
//        // Add method specification if necessary
//        resourceName = request.getMethod().toUpperCase() + ":" + resourceName;
//        return resourceName;
//    }
//
//}
